import {count} from "@/common/utils/count.js"

//获取sku列表
export const findSpecList = function (skuList) {
	//组装规格名称
	var valueList = []
	var specNames = new Set()
	for(var item of skuList){
		for(var value of JSON.parse(item.specValues)){
			specNames.add(value.specName)
			valueList.push(value)
		}
	}
	//组装规格
	var specList = []
	var specs_sel = ''
	for(var specName of specNames){
		var sku = {}
		var specSet = new Set()
		for(var item of valueList){
			if(specName == item.specName){
				specSet.add(item.name)
			}
		}
		sku.specName = specName
		//组装规格值
		var specListTemp = Array.from(specSet)
		var specValueList = []
		for(var i in specListTemp){
			var name = specListTemp[i]
			var spec = {}
			spec.specName = specName
			spec.name = name
			spec.select = false
			//设置默认选中的规格
			// for(var sel of JSON.parse(this.itemModel.defaultSku.specValues)){
			// 	if(sel.name == name){
			// 		spec.select = true
			// 		specs_sel = specs_sel + name + '，'
			// 	}
			// }
			specValueList.push(spec)
		}
		sku.specValueList = specValueList
		specList.push(sku)
	}
	return specList
	// this.$set(this.itemModel, 'specList', specList)
	// // this.itemModel.specList = specList
	// specs_sel = specs_sel.substring(0, specs_sel.length-1)
}

export const getSpecList_sel = function (specList) {
	//组装specList
	var specList_sel = []
	for (var item2 of specList) {
		for (var spec2 of item2.specValueList) {
			if (spec2.select) {
				var value = {}
				value.specName = spec2.specName
				value.name = spec2.name
				specList_sel.push(value)
			}
		}
	}
	return specList_sel
}
export const getSpecs_sel = function (specList_sel) {
	//将选中的spec以,隔开
	var specs_sel = ''
	for(var item of specList_sel){
		specs_sel = specs_sel + item.name + '，'
	}
	specs_sel = specs_sel.substring(0, specs_sel.length-1)
	return specs_sel
}

export const goCreateOrderParams = function (itemModel){
	// console.log('去下单', this.itemModel)
	// 添加购物车商品
	var item = itemModel
	var shopCartItems = []
	var obj = {}
	obj.item = item.id
	obj.itemName = item.name
	if(item.skuList.length==1){
		obj.sku = item.skuList[0].id
	}else{
		obj.sku = item.skuId
	}
	obj.specValues = item.specValues
	obj.payCount = item.payCount
	// obj.sellingPrice = item.sellingPrice
	obj.priceModel = item.priceModel
	obj.coverUrl = item.coverUrl
	
	shopCartItems.push(obj)
	var priceModel = {
		sellingPrice: count.mul(item.priceModel.sellingPrice, item.payCount),
	}				
	var params = {
		shopId: itemModel.shop||'',
		orderItemList: encodeURIComponent(JSON.stringify(shopCartItems)),
		priceModel: encodeURIComponent(JSON.stringify(priceModel)),
	}
	if(item.activity){
		params.activityModel = encodeURIComponent(JSON.stringify(itemModel.activityModel))
	}
	if(item.activityKey=='incomesDeduction'){
		params.realpayIncomes = count.mul(item.incomesPrice, item.payCount)
	}
	params.scene = 'generay'
	return params
}